package com.example.surveyapplication;

import java.util.ArrayList;
import java.util.List;

import object.ResultObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ResultListViewAdapter extends ArrayAdapter<ResultObject> {

	ArrayList<ResultObject> objects;
	Context context;

	public ResultListViewAdapter(Context context, List<ResultObject> objects) {
		super(context, R.layout.item_result_listview, objects);
		// TODO Auto-generated constructor stub
		this.objects = (ArrayList<ResultObject>) objects;
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		if (view == null)
			view = new ItemResultListView(context);

		final ResultObject resultObject = objects.get(position);
		if (resultObject != null) {

			TextView tvOrder = ((ItemResultListView) view).tvOrder;
			TextView tvFirstName = ((ItemResultListView) view).tvFirstName;
			TextView tvSurname = ((ItemResultListView) view).tvSurname;
			TextView tvStress = ((ItemResultListView) view).tvStress;
			TextView tvDepression = ((ItemResultListView) view).tvDepression;
			TextView tvAnxiety = ((ItemResultListView) view).tvAnxiety;
			TextView tvDate = ((ItemResultListView) view).tvDate;

			tvOrder.setText(resultObject.getOrder());
			tvFirstName.setText(resultObject.getFirstName());
			tvSurname.setText(resultObject.getSurName());
			tvStress.setText(resultObject.getStress());
			tvDepression.setText(resultObject.getDepression());
			tvAnxiety.setText(resultObject.getAnxiety());
			tvDate.setText(resultObject.getDate());

		}

		return view;
	}

	private class ItemResultListView extends LinearLayout {

		TextView tvOrder;
		TextView tvFirstName;
		TextView tvSurname;
		TextView tvStress;
		TextView tvDepression;
		TextView tvAnxiety;
		TextView tvDate;

		public ItemResultListView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			LayoutInflater listItem = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			listItem.inflate(R.layout.item_result_listview, this, true);

			this.tvOrder = (TextView) findViewById(R.id.tvOrder);
			this.tvFirstName = (TextView) findViewById(R.id.tvFirstName);
			this.tvSurname = (TextView) findViewById(R.id.tvSurname);
			this.tvStress = (TextView) findViewById(R.id.tvStress);
			this.tvDepression = (TextView) findViewById(R.id.tvDepression);
			this.tvAnxiety = (TextView) findViewById(R.id.tvAnxiety);
			this.tvDate = (TextView) findViewById(R.id.tvDate);

		}

	}

}
